yutool-mq消息组件
快速上手
1. Maven依赖
在项目的 pom.xml
中添加以下依赖:
xml
<parent>
<groupId>com.yupaits</groupId>
<artifactId>yutool-parent</artifactId>
<version>${yutool.version}</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>com.yupaits</groupId>
<artifactId>yutool-mq</artifactId>
</dependency>
</dependencies>
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
2. 队列初始化
编写消息队列枚举 SampleQueue
:
java
@Getter
public enum SampleQueue implements IQueueEnum {
/**
* 普通队列
*/
SAMPLE_QUEUE("sample.exchange", "sample", "sample"),
/**
* 延迟队列
*/
SAMPLE_TTL_QUEUE("sample.exchange.ttl", "sample.ttl", "sample.ttl");
private String exchange;
private String name;
private String routeKey;
SampleQueue(String exchange, String name, String routeKey) {
this.exchange = exchange;
this.name = name;
this.routeKey = routeKey;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
在项目中编写 MqConfig
配置类用于注册队列相关 Bean:
java
@Configuration
public class MqConfig {
@Bean
public DirectExchange sampleExchange() {
return MqUtils.direct(SampleQueue.SAMPLE_QUEUE);
}
@Bean
public DirectExchange sampleTtlExchange() {
return MqUtils.direct(SampleQueue.SAMPLE_TTL_QUEUE);
}
@Bean
public Queue sampleQueue() {
return MqUtils.queue(SampleQueue.SAMPLE_QUEUE);
}
@Bean
public Queue sampleTtlQueue() {
return MqUtils.ttlQueue(SampleQueue.SAMPLE_TTL_QUEUE, SampleQueue.SAMPLE_QUEUE);
}
@Bean
public Binding sampleBinding() {
return MqUtils.binding(sampleExchange(), sampleQueue(), SampleQueue.SAMPLE_QUEUE);
}
@Bean
public Binding sampleTtlBinding() {
return MqUtils.binding(sampleTtlExchange(), sampleTtlQueue(), SampleQueue.SAMPLE_TTL_QUEUE);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
3. 消息发送
java
@Slf4j
public class MqTest1 {
@Autowired
private Sender sender;
public void testSendMessage() {
sender.sendMessage("Hello, MQ!", SampleQueue.SAMPLE_QUEUE);
}
public void testSendDelayMessage() {
sender.sendDelayMessage("Hello, Delayed MQ!", SampleQueue.SAMPLE_TTL_QUEUE, 5000L);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
java
@Slf4j
public class MqTest2 {
//开启缓存服务时,可以注入RetryableSender
@Autowired
private RetryableSender sender;
public void testSendMessage() {
sender.sendMessage("Hello, MQ!", SampleQueue.SAMPLE_QUEUE);
}
public void testSendDelayMessage() {
sender.sendDelayMessage("Hello, Delayed MQ!", SampleQueue.SAMPLE_TTL_QUEUE, 5000L);
}
public void testSendRetryableMessage() throws MqRetryException {
sender.sendRetryableMessage("Hello, Retryable MQ!", SampleQueue.SAMPLE_QUEUE, RetryProps.builder()
.retryable(true)
.times(3)
.strategy(RetryStrategy.PERIODIC)
.firstDelayMillis(3000)
.intervalMillis(1000)
.build());
}
public void testSendDelayRetryableMessage() throws MqRetryException {
sender.sendDelayRetryableMessage("Hello, Delay retryable MQ!", SampleQueue.SAMPLE_TTL_QUEUE, 5000L, RetryProps.builder()
.retryable(true)
.times(3)
.strategy(RetryStrategy.PROGRESSIVE)
.delays(Lists.newArrayList(3000L, 10000L, 20000L))
.build());
log.info("发送延迟重试消息");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
4. 消息接收
java
@Slf4j
@Component
@RabbitListener(queues = "sample")
public class SampleMessageReceiver implements Receiver<String> {
@RabbitHandler
@Override
public void handle(String message) {
log.info("接收消息:{}", message);
}
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
评论区留言准则:
1. 本评论区禁止传播封建迷信、吸烟酗酒、低俗色情、赌博诈骗等任何违法违规内容。
2. 当他人以不正当方式诱导打赏、私下交易,请谨慎判断,以防人身财产损失。
3. 请勿轻信各类招聘征婚、代练代抽、私下交易、购买礼包码、游戏币等广告信息,谨防网络诈骗。